package com.jt.security.interceptor;

import com.jt.security.util.JwtUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 令牌(token:ticker-通票)拦截器
 * 其中:HandlerInterceptor为springmvc中的拦截器
 * 可以在controller方法执行之前之后执行一些操作
 * 1.Handler处理器()
 * 2.interceptor 拦截器
 *
 * */
public class TokenInterceptor implements HandlerInterceptor {
    /**
     * preHandle在目标Controller方法执行之前执行
     * @param handler 目标Controller对象
     */

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {


        System.out.println("====preHandle====");
        //String token = request.getParameter("Authentication");
        String token = request.getHeader("Authentication");
        //判定请求中是否有令牌
        if (token == null || token == "")
            throw new RuntimeException("please login");
        boolean flag = JwtUtils.isTokenExpired(token);
        //判定令牌是否过期
        if (flag)
            throw new RuntimeException("login timeout");
        return true;//false表示拦截到请求后,不再继续传递
    }
}
